package jp.co.ricoh.melody.presenters;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import jp.co.ricoh.melody.databases.DBManager;
import jp.co.ricoh.melody.models.AlarmModel;
import jp.co.ricoh.melody.models.entities.AlarmData;
import jp.co.ricoh.melody.views.adapters.AlarmListView;
import jp.co.ricoh.melody.views.adapters.IAlarmListView;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 */
public class AlarmListPresenter extends RecyclerView.Adapter<AlarmListView.AlarmItemViewHolder>
        implements IAlarmListPresenter, AlarmModel.OnAlarmDataSetChangedListener {
    public static final String TAG = AlarmListPresenter.class.getSimpleName();
    private IAlarmListView mIAlarmListView;
    private AlarmModel mAlarmModel;

    private List<AlarmData> mAlarms = new ArrayList<>();

    public AlarmListPresenter() {
        mAlarmModel = AlarmModel.getInstance();
        mAlarms.clear();
        mAlarms.addAll(mAlarmModel.getAlarmList());
        mAlarmModel.addOnAlarmDataSetChangedListener(this);
    }

    private int getPositionById(int alarmId) {
        for (int i = 0; i < mAlarms.size(); ++i) {
            if (mAlarms.get(i).getId() == alarmId) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void onAlarmAdded(int alarmId) {
        AlarmData newAlarm = mAlarmModel.getAlarmById(alarmId);
        if (mAlarms.isEmpty()) {
            mAlarms.add(newAlarm);
        } else {
            mAlarms.add(0, newAlarm);
        }
        notifyDataSetChanged();
        if (mIAlarmListView != null) {
            mIAlarmListView.scrollUp();
        }
    }

    @Override
    public void onAlarmDeleted(int alarmId) {
        int position = getPositionById(alarmId);
        Iterator<AlarmData> alarmDataIterator = mAlarms.iterator();
        while (alarmDataIterator.hasNext()) {
            if (alarmDataIterator.next().getId() == alarmId) {
                alarmDataIterator.remove();
            }
        }
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, 1);
    }

    @Override
    public void onAlarmChanged(int alarmId) {
        int position = getPositionById(alarmId);
        mAlarms.set(position, mAlarmModel.getAlarmById(alarmId));
        notifyItemChanged(position);
    }

    @Override
    public void loadData() {
        notifyDataSetChanged();
    }

    @Override
    public void onCreateView(IAlarmListView view) {
        this.mIAlarmListView = view;
        if (mIAlarmListView != null) {
            mIAlarmListView.setAdapter(this);
        }
    }

    @Override
    public void onDestroyView() {
        mAlarmModel.removeOnAlarmDataSetChangedListener(this);
        this.mIAlarmListView = null;
    }

    @Override
    public void setEnableAlarm(boolean enabled, AlarmData alarmData, DBManager.OnSaveDataResultListener onSaveDataResultListener) {
        mAlarmModel.enableAlarm(enabled, alarmData, onSaveDataResultListener);
    }

    @Override
    public void deleteAlarm(AlarmData alarmData, DBManager.OnSaveDataResultListener onSaveDataResultListener) {
        mAlarmModel.deleteAlarm(alarmData, onSaveDataResultListener);
    }

    @Override
    public void cancelAlarmFromAlarmManager(int alarmId) {
        if (mIAlarmListView != null) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mIAlarmListView.getContext(), alarmId, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) mIAlarmListView.getContext().getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);
        }
    }

    @Override
    public void editAlarm(int alarmId) {
        if (mIAlarmListView != null) {
            mIAlarmListView.navigateToEditAlarmActivity(alarmId);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public AlarmListView.AlarmItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        AlarmListView.AlarmItemViewHolder holder = mIAlarmListView.onCreateViewHolder(parent, viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(AlarmListView.AlarmItemViewHolder holder, int position) {
        mIAlarmListView.onBindViewHolder(holder, mAlarms.get(position));

    }

    @Override
    public int getItemCount() {
        return mAlarms.size();
    }
}
